﻿
@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    @*<link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">*@
    <link href="~/layuimini/lib/layui-v2.6.3/css/layui.css" rel="stylesheet" />
    @*<link rel="stylesheet" href="../css/public.css" media="all">*@
    <link href="~/layuimini/css/public.css" rel="stylesheet" />
</head>
<body>
    <div class="layuimini-container">
        <div class="layuimini-main">

            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">商品类别名称</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="CategoryName" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>

            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
                </div>
            </script>

            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

            @*系统管理员*@
            <script type="text/html" id="currentTableBar1">
                <a class="layui-btn layui-btn-xs layui-btn-warm data-count-delete" lay-event="delivery">发货</a>
                <a class="layui-btn layui-btn-xs layui-btn-normal data-count-delete" lay-event="receipts">确认收货</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="receipt">配送</a>
            </script>

            @*商家*@
            <script type="text/html" id="currentTableBar2">
                {{#  if(d.state == '已下单'){ }}
                <a class="layui-btn layui-btn-xs layui-btn-warm data-count-delete" lay-event="delivery">发货</a>
                {{#  }else if(d.state == '已退回仓库'){ }}
                <a class="layui-btn layui-btn-xs layui-btn-normal data-count-delete" lay-event="refund">退款</a>

                {{#  }else { }}
                <a class="layui-btn layui-btn-xs layui-btn-disabled">发货</a>
                <a class="layui-btn layui-btn-xs layui-btn-disabled">退款</a>
                {{#  } }}
            </script>

            @*用户*@
            <script type="text/html" id="currentTableBar3">
                {{#  if(d.state == '已送达'){ }}
                <a class="layui-btn layui-btn-xs layui-btn-normal data-count-delete" lay-event="receipts">确认收货</a>

                {{#  }else if(d.state == '订单已完成'){ }}
                <a class="layui-btn layui-btn-xs layui-btn-normal data-count-delete" lay-event="evaluate">评价</a>
                <a class="layui-btn layui-btn-xs layui-btn-normal data-count-delete" lay-event="salesreturn">退货</a>

                {{#  }else if(d.state == '同意退货'){ }}
                <a class="layui-btn layui-btn-xs layui-btn-normal data-count-delete" lay-event="turndestock">货物退回</a>

                {{#  }else { }}
                <a class="layui-btn layui-btn-xs layui-btn-disabled">确认收货</a>
                {{#  } }}

            </script>


            @*配送员*@
            <script type="text/html" id="currentTableBar4">
                {{#  if(d.state == '已发货'){ }}
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="receipt">配送</a>
                {{#  }else { }}
                <a class="layui-btn layui-btn-xs layui-btn-disabled">配送</a>
                {{#  } }}
            </script>




        </div>
    </div>
    @*<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>*@
    <script src="~/layuimini/lib/layui-v2.6.3/layui.js"></script>

    <script>
        var tableIns;
        var OrderInfoId;


        //table实例
        var table;
        var form;
        var $;

        layui.use(['form', 'table'], function () {
            $ = layui.jquery;
            form = layui.form;
            table = layui.table;



            $.ajax({
                url: 'http://localhost:31861/api/UserInfo/IsUserRole',
                type: 'get',
                xhrFields: {
                    withCredentials: true
                },
                success: function (res) {
                    for (var i = 0; i < res.datas.length; i++) {
                        if (res.datas[i].roleName == "用户") {
                            initView("用户")
                        } else if (res.datas[i].roleName == "商家") {
                            initView("商家")
                        } else if (res.datas[i].roleName == "配送员") {
                            initView("配送员")
                        } else if (res.datas[i].roleName == "系统管理员") {
                            initView("系统管理员")

                        }
                    }
                }
            });


            //初始化页面
            function initView(state) {
                var arr;
                var toolbar = '';
                if (state == "系统管理员") {
                    arr = [[
                        { type: "checkbox", width: 50 },
                        { field: 'id', title: 'ID', hide: true, sort: true },
                        { field: 'commodityName', width: 120, title: '商品名称' },
                        { field: 'num', width: 120, title: '数量' },
                        { field: 'amount', width: 120, title: '订单金额' },
                        { field: 'state', width: 120, title: '订单状态' },
                        { field: 'userName', width: 120, title: '购买的用户' },
                        { field: 'orderTime', width: 120, title: '购买时间' },
                        { title: '操作', minWidth: 150, toolbar: '#currentTableBar1', align: "center" }
                    ]];
                } else if (state == "商家") {
                    arr = [[
                        { type: "checkbox", width: 50 },
                        { field: 'id', title: 'ID', hide: true, sort: true },
                        { field: 'commodityName', width: 120, title: '商品名称' },
                        { field: 'num', width: 120, title: '数量' },
                        { field: 'amount', width: 120, title: '订单金额' },
                        { field: 'state', width: 120, title: '订单状态' },
                        { field: 'userName', width: 120, title: '购买的用户' },
                        { field: 'orderTime', width: 120, title: '购买时间' },
                        { title: '操作', minWidth: 150, toolbar: '#currentTableBar2', align: "center" }
                    ]];
                } else if (state == "用户") {
                    arr = [[
                        { type: "checkbox", width: 50 },
                        { field: 'id', title: 'ID', hide: true, sort: true },
                        { field: 'commodityName', width: 120, title: '商品名称' },
                        { field: 'num', width: 120, title: '数量' },
                        { field: 'amount', width: 120, title: '订单金额' },
                        { field: 'state', width: 120, title: '订单状态' },
                        { field: 'userName', width: 120, title: '购买的用户' },
                        { field: 'orderTime', width: 120, title: '购买时间' },
                        { title: '操作', minWidth: 150, toolbar: '#currentTableBar3', align: "center" }
                    ]];
                } else if (state == "配送员") {
                    arr = [[
                        { type: "checkbox", width: 50 },
                        { field: 'id', title: 'ID', hide: true, sort: true },
                        { field: 'commodityName', width: 120, title: '商品名称' },
                        { field: 'num', width: 120, title: '数量' },
                        { field: 'amount', width: 120, title: '订单金额' },
                        { field: 'state', width: 120, title: '订单状态' },
                        { field: 'userName', width: 120, title: '购买的用户' },
                        { field: 'orderTime', width: 120, title: '购买时间' },
                        { title: '操作', minWidth: 150, toolbar: '#currentTableBar4', align: "center" }
                    ]];

                } else {

                }

                //渲染table并且获取实例
                tableIns = table.render({
                    elem: '#currentTableId',
                    url: 'http://localhost:31861/api/OrderInfo/OrderInfoPageList',
                    toolbar: '#toolbarDemo',
                    defaultToolbar: ['filter', 'exports', 'print', {
                        title: '提示',
                        layEvent: 'LAYTABLE_TIPS',
                        icon: 'layui-icon-tips'
                    }],
                    cols: arr,
                    limits: [5, 10, 15, 20, 25, 50],
                    limit: 5,
                    page: true,
                    skin: 'line'
                });

                // 监听搜索操作
                form.on('submit(data-search-btn)', function (data) {
                    var result = JSON.stringify(data.field);

                    tableIns.reload({
                        where: { //设定异步数据接口的额外参数，任意设
                            categoryName: data.field.CategoryName,
                            //…
                        },
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });

                    return false;
                });

                /**
                 * toolbar监听事件
                 */
                table.on('toolbar(currentTableFilter)', function (obj) {
                    if (obj.event === 'delete') {  // 监听删除操作
                        var checkStatus = table.checkStatus('currentTableId');
                        var data = checkStatus.data;

                        var ids = [];
                        for (var i = 0; i < data.length; i++) {
                            ids.push(data[i].id);
                        }

                        //发起请求批量删除商品类别
                        $.ajax({
                            url: 'http://localhost:31861/api/OrderInfo/DeleteCommodityInfos',
                            type: 'post',
                            data: {
                                CategoryIds: ids
                            },
                            success: function (res) {
                                if (res.isSuccess) {
                                    //刷新表格
                                    reLoadTable();
                                }
                                //提示信息
                                layer.msg(res.msg);
                            }
                        });
                    }
                });

                //监听表格复选框选择
                table.on('checkbox(currentTableFilter)', function (obj) {
                    console.log(obj);
                });

                table.on('tool(currentTableFilter)', function (obj) {
                    var data = obj.data;
                    if (obj.event === 'delete') {
                        console.log(obj);
                        layer.confirm('真的删除行么', function (index) {
                            //发起请求，删除数据库中的用户
                            $.ajax({
                                url: 'http://localhost:31861/api/OrderInfo/DeleteOrderInfo',
                                data: { 'OrderInfoId': obj.data.id },
                                type: 'post',
                                success: function (res) {
                                    if (res.isSuccess) {
                                        layer.msg(res.msg);
                                        //刷新表格
                                        reLoadTable();
                                    }
                                    else {
                                        layer.msg(res.msg);
                                    }

                                }
                            });
                            layer.close(index);
                        });
                    }
                    //接单
                    else if (obj.event === 'receipt') {
                        console.log(obj);
                        //发起接单请求
                        $.ajax({
                            url: '/OrderInfo/Receipt',
                            type: 'post',
                            data: {
                                orderInfoId: obj.data.id
                            },
                            success: function (res) {
                                if (res.isSuccess) {
                                    //刷新表格
                                    reLoadTable();
                                }
                                //提示信息
                                layer.msg(res.msg);
                            }
                        });
                    }//发货
                    else if (obj.event === 'delivery') {
                        console.log(obj);
                        //发起接单请求
                        $.ajax({
                            url: '/OrderInfo/Delivery',
                            type: 'post',
                            data: {
                                orderInfoId: obj.data.id
                            },
                            success: function (res) {
                                if (res.isSuccess) {
                                    //刷新表格
                                    reLoadTable();
                                }
                                //提示信息
                                layer.msg(res.msg);
                            }
                        }); //收货货
                    } else if (obj.event === 'receipts') {
                        console.log(obj);
                        //发起接单请求
                        $.ajax({
                            url: '/OrderInfo/Receipts',
                            type: 'post',
                            data: {
                                orderInfoId: obj.data.id
                            },
                            success: function (res) {
                                if (res.isSuccess) {
                                    //刷新表格
                                    reLoadTable();
                                }
                                //提示信息
                                layer.msg(res.msg);
                            }
                        });
                    }//退货
                    else if (obj.event === 'salesreturn') {
                        console.log(obj);
                        //发起接单请求
                        $.ajax({
                            url: '/OrderInfo/Salesreturn',
                            type: 'post',
                            data: {
                                orderInfoId: obj.data.id
                            },
                            success: function (res) {
                                if (res.isSuccess) {
                                    orderInfoId = obj.data.id;
                                    var index = layer.open({
                                        title: '申请退货',
                                        type: 2,
                                        shade: 0.2,
                                        maxmin: true,
                                        shadeClose: true,
                                        area: ['600px', '500px'],
                                        content: '/OrderInfo/AddSalesreturnView'
                                    });
                                    $(window).on("resize", function () {
                                        layer.full(index);
                                    });


                                } else {
                                    //提示信息
                                    layer.msg(res.msg);

                                }
                            }
                        });
                    } else if (obj.event === 'evaluate') {
                        orderInfoId = obj.data.id;
                        var index = layer.open({
                            title: '评价',
                            type: 2,
                            shade: 0.2,
                            maxmin: true,
                            shadeClose: true,
                            area: ['550px', '500px'],
                            content: '/EvaluateInfo/AddEvaluateInfoView'
                        });
                        $(window).on("resize", function () {
                            layer.full(index);
                        });
                    } else if (obj.event === 'turndestock') {  //退回

                        $.ajax({
                            url: 'http://localhost:31861/api/OrderInfo/Turndestock',
                            type: 'post',

                            data: {
                                orderInfoId: obj.data.id
                            },
                            success: function (res) {
                                if (res.isSuccess) {
                                    //刷新表格
                                    reLoadTable();
                                } 信息
                                layer
                                //提示.msg(res.msg);
                            }
                        });
                    } else if (obj.event === 'refund') {  //退款

                        $.ajax({
                            url: 'http://localhost:31861/api/OrderInfo/Refund',
                            type: 'post',
                            data: {
                                orderInfoId: obj.data.id
                            },
                            success: function (res) {
                                if (res.isSuccess) {
                                    //刷新表格
                                    reLoadTable();
                                } 信息
                                layer
                                //提示.msg(res.msg);
                            }
                        });
                    }




                });



            }





        });

        //刷新表格
        function reLoadTable() {
            tableIns.reload({
                //where: { //设定异步数据接口的额外参数，任意设
                //    aaaaaa: 'xxx'
                //    , bbb: 'yyy'
                //    //…
                //}
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        }
    </script>

</body>
</html>